/**
 * Bind events to controls
 */
var bind = {
	
	stopBubble : function(e) {
		debug.print("bind.stopBubble");
		if (!e) var e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	},
	
	init : function() {
		debug.print("bind.init");
		
		/**
		 * Ajax error
		 */
		$(document).ajaxError(function(e, xhr, settings, exception) {
  			// console.log(settings.url)
    		flash.modal("Error", "An error occured during your request. Please try again.");
			// TODO: STOP ALL LOADING STATES IN UI
		});
		
		/**
		 * play/pause buttons
		 */
		$("#vid0-pp-button").click(function() {
			controller.togglePlay(0);
		});
		$("#vid1-pp-button").click(function() {
			controller.togglePlay(1);
		});
		
		/**
		 * click on video screen to start
		 */
		$("#vid0-wrapper").click(function() {
			if ($(this).hasClass("ready")) {
				controller.togglePlay(0);
			}			
		});
		$("#vid1-wrapper").click(function() {
			if ($(this).hasClass("ready")) {
				controller.togglePlay(1);
			}			
		});
		
		/**
		 * Toggle current/remaining time
		 */
		$("#vid0-time").click(function() {
			player[0].displayRemainingTime = !player[0].displayRemainingTime;
			player[0].updateTime();			
		});
		$("#vid1-time").click(function() {
			player[1].displayRemainingTime = !player[1].displayRemainingTime;
			player[1].updateTime();			
		});
		
		
		/**
		 * reset fader
		 */ 
		$("#fader-container").dblclick(function() {
			controller.faderReset();
		});
		
		/**
		 * enhance
		 */ 
		$("#vid0-enhance-button a, #vid0-title a, #vid0-actions > div.enhance a.enhance-cancel").click(function(e) {
			bind.stopBubble(e);
			enhance.toggle(0);
		});
		$("#vid1-enhance-button a, #vid1-title a, #vid1-actions > div.enhance a.enhance-cancel").click(function(e) {
			bind.stopBubble(e);
			enhance.toggle(1);
		});
		$(".enhance-flags a").click(function(e) {
			enhance.change(this);
			enhance.toggleTag(this);
		});
		$(".enhance-artist, .enhance-title").keyup(function(e) {
			enhance.change(this);
		});
		$("#vid0-actions > div.enhance, #vid1-enhance").click(function(e) {
			bind.stopBubble(e);
		});
		$("#vid0-actions > div.enhance a.enhance-save").click(function(e) {
			bind.stopBubble(e);
			enhance.save(0);
		});
		$("#vid1-actions > div.enhance a.enhance-save").click(function(e) {
			bind.stopBubble(e);
			enhance.save(1);
		});
		
		/**
		 * share
		 */ 
		$("#vid0-share-button a, #vid0-actions > div.share a.share-hide").click(function(e) {
			bind.stopBubble(e);
			share.toggle(0);
		});
		$("#vid1-share-button a, #vid1-actions > div.share a.share-hide").click(function(e) {
			bind.stopBubble(e);
			share.toggle(1);
		});
        $("div.addthis a").click(function(e) {
			bind.stopBubble(e);
		});
		
		/**
		 * fav
		 */
		$("#vid0-fav-button a").click(function(e) {
			bind.stopBubble(e);
			fav.add(0);
		});
		$("#vid1-fav-button a").click(function(e) {
			bind.stopBubble(e);
			fav.add(0);
		});
		
		/**
		 * download
		 */
		$("#vid0-download-button a").click(function(e) {
			bind.stopBubble(e);
			download.download(0);
		});
		$("#vid1-download-button a").click(function(e) {
			bind.stopBubble(e);
			download.download(1);
		});
		
		/**
		 * tabs
		 */
		$("#tab-info").click(function(e) {
			tabs.show("info");
		});
		$("#tab-search").click(function(e) {
			tabs.show("search");
		});
		$("#tab-playlist").click(function(e) {
			tabs.show("playlist");
		});
		
		/**
		 * search
		 */ 
		$("#search-field").keydown(function(e) {
			if (e.keyCode === 13) {
				controller.doVideoSearch();
			}
		});
		$("#search-button").click(function() {
			controller.doVideoSearch();
		});
		$("#lucky-button").click(function() {
			var search = $("#search-field > :input").val();
			if (!search) {
				return flash.flash("No search","query given");
			}
			ui.setSearchStatus('loading');
			controller.beLucky(search, function() {
				ui.setSearchStatus('idle');
			});
		});
		$("#search-next a").click(function() {
			controller.doVideoSearch();
		});
		
		/**
		 * playlist
		 */
		
		$("#featured-lists li").click(function() {
			var list_id = $(this).attr("id").substr(9,1);
			autodj.disable();
			np.empty();
			playlist.load(list_id, function() {
				autodj.source = 'np';
				autodj.initialized = false;
				autodj.enable();
			});
		});	
		
		/**
		 * autodj
		 */ 
		$("#button-autodj").click(function() {
			controller.toggleAutodj();
		});
		
		$("#autodj-controls").click(function(e) {
			bind.stopBubble(e);
		});
		$(".autodj-mix").click(function() {
			controller.mixNow();
		});
		$(".autodj-next0").click(function() {
			controller.cueNext(0);
		});
		$(".autodj-next1").click(function() {
			controller.cueNext(1);
		});
		
		$(".autodj-next0").mouseenter(function() {
			autodj.showLabel(0);
		});
		$(".autodj-next0").mouseleave(function() {
			autodj.hideLabel(0);
		});
		$(".autodj-next1").mouseenter(function() {
			autodj.showLabel(1);
		});
		$(".autodj-next1").mouseleave(function() {
			autodj.hideLabel(1);
		});
				
		/**
		 * drag and drop
		 */
		this.np_sort();
		this.np_drop();
		this.player_drop();
		
		/**
		 * launcher bind
		 */
		launcher.bind();
	},
	
	np_drop : function() {
		debug.print("bind.np_drop");
		$("#side").droppable({
			accept: ".result, .thumb",
			activeClass: 'highlight',
			hoverClass: 'drop-hover',
			tolerance: 'touch',
			drop: function(e, ui) {
				controller.addToNp(ui.draggable.data("video"));
			}
		});
	},
	
	player_drop : function() {
		debug.print("bind.player_drop");
		$("#vid0-wrapper, #vid1-wrapper").droppable({
			accept: ".pl-item, .result, .thumb",
			activeClass: 'highlight',
			hoverClass: 'drop-hover',
			tolerance: 'pointer',
			drop: function(e, ui) {
				player_key = $(this).attr("id").substr(3,1);
				controller.cue(player_key, ui.draggable.data("video"));
				if (ui.draggable.hasClass("pl-item")) {
					ui.draggable.hide();
					controller.removeFromNp(ui.draggable, ui.draggable.data("video"));	
				}
			}
		});
	},
	
	search_videos_drag : function() {
		debug.print("bind.search_videos_dnd");
		$("#search-results .result").draggable({
			helper: function() {
				return $(this).children(".result-cover").clone();
			},
			cursorAt: { left: 30, top: 25 }
		});		
	},
	
	recent_videos_drag : function() {
		debug.print("bind.recent_videos_drag");
		$("#recentThumbs .thumb").draggable({
			helper: function() {
				var clone = $(this).clone();
				$("body").prepend(clone);
				return clone;
			}
		});		
	},
	
	np_sort : function() {
		debug.print("bind.np_sort");
		$("#np").sortable({
			tolerance: 'pointer'
		});
	}
	
}